<?php

namespace app\admin\model;

use think\Model;


class SchoolClass extends Model
{


    // 表名
    protected $name = 'school_class';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'start_time_text',
        'status_text',
        'end_time_text'
    ];


    public function getStatusList()
    {
        return ['0' => __('Status 0'), '2' => __('Status 2')];
    }

    public function geisleaveList()
    {
        return ['0' => '否', '1' => '是'];
    }
    public function geclassdiscountList()
    {
        return ['0' => '关闭', '1' => '一口价优惠','2'=>'折扣优惠','3'=>'优惠课时'];
    }

    public function getStartTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['start_time']) ? $data['start_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getEndTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['end_time']) ? $data['end_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setStartTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setEndTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function admin()
    {
        return $this->belongsTo('Admin', 'admin_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function schoolclasstype()
    {
        return $this->belongsTo('SchoolClassType', 'class_type', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function schoolclasssubject()
    {
        return $this->belongsTo('school_class_subject', 'id', 'class_id', [], 'LEFT')->setEagerlyType(0);
    }
}
